document.getElementById('exportPdf').addEventListener('click', function() {
    // 显示加载提示
    const btn = document.getElementById('exportPdf');
    btn.disabled = true;
    btn.textContent = '正在生成PDF...';
    
    const element = document.getElementById('content');
    const options = {
        scale: 2,
        useCORS: true,
        allowTaint: false,
        scrollX: 0,
        scrollY: 0,
        windowWidth: element.scrollWidth,
        windowHeight: element.scrollHeight,
        logging: true,
        backgroundColor: '#ffffff',
        letterRendering: true,
        ignoreElements: function(el) {
            return el.tagName === 'SCRIPT';
        }
    };
    
    // 添加临时样式确保完整渲染
    const originalStyles = {
        overflow: element.style.overflow,
        position: element.style.position
    };
    element.style.overflow = 'visible';
    element.style.position = 'static';
    
    html2canvas(element, options).then(canvas => {
        // 恢复原始样式
        element.style.overflow = originalStyles.overflow;
        element.style.position = originalStyles.position;
        
        const imgData = canvas.toDataURL('image/jpeg', 0.95);
        const pdf = new jsPDF('p', 'mm', 'a4');
        const pdfWidth = pdf.internal.pageSize.getWidth() - 20;
        const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
        
        // 计算分页
        let position = 0;
        let remainingHeight = pdfHeight;
        
        pdf.addImage(imgData, 'JPEG', 10, position, pdfWidth, pdfHeight);
        remainingHeight -= pdf.internal.pageSize.getHeight();
        
        while (remainingHeight > 0) {
            position = remainingHeight - pdfHeight + 10;
            pdf.addPage();
            pdf.addImage(imgData, 'JPEG', 10, position, pdfWidth, pdfHeight);
            remainingHeight -= pdf.internal.pageSize.getHeight();
        }
        
        pdf.save('微服务架构介绍.pdf');
    }).catch(error => {
        console.error('PDF导出错误:', error);
        alert('PDF导出失败: ' + error.message);
    }).finally(() => {
        btn.disabled = false;
        btn.textContent = '导出PDF';
    });
});